package com.unsk.state
{
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2World;
	
	import com.alex.game.framework.BasicItem;
	import com.alex.game.framework.BasicState;
	import com.alex.game.framework.GG;
	import com.alex.game.framework.WorldFabrique;
	import com.unsk.item.IUpdatable;
	import com.unsk.item.player.Player;
	import com.unsk.item.Box2DBodyContactListener;
	import com.unsk.item.player.PlayerInputDecorator;
	import com.unsk.util.Log;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	public class PlayState extends BasicState
	{
		
		private var worldFabrique:WorldFabrique;
		override public function create():void
		{
			initWorldFabrique();
			createWorld();
			createDecor();
		}
		override public function destroy():void
		{
			worldFabrique.destroyWorld();
		}
		override public function update():void
		{
			super.update();
			worldFabrique.update(1/(GG.elapsedTime/2), 10);
		}
		/**
		 * Initialise la fabrique du monde box2D 
		 * 
		 */		
		private function initWorldFabrique():void
		{
			worldFabrique = new WorldFabrique();
		}
		/**
		 * Créé le monde 
		 * 
		 */		
		private function createWorld():void
		{
			
			var debugSprite:Sprite = new Sprite();
			worldFabrique.createWorld(debugSprite);
			
			addChild(debugSprite);
			
		}
		/**
		 * Créé les élèments du monde 
		 * 
		 */		
		private function createDecor():void
		{
			createWallsAndFloor();
			createBox();
			createPlayer();
		}
		private function createWallsAndFloor():void
		{
			worldFabrique.addWall(0,470,640,10);
			worldFabrique.addWall(0,0,10,480);
			worldFabrique.addWall(0,0,640,10);
			worldFabrique.addWall(630,0,10,480);
			worldFabrique.addWall(0,200,200,10);
			worldFabrique.addWall(200,250,200,10);
			worldFabrique.addWall(250,300,30,10);
			worldFabrique.addWall(280,350,30,10);
			worldFabrique.addWall(310,400,30,10);
		}
		private function createBox():void
		{
			
		}
		private function createPlayer():void
		{
			var playerBody:b2Body = worldFabrique.addCircle(50,50,30,1);
			var player:IUpdatable = new PlayerInputDecorator(new Player(playerBody),worldFabrique.world);
			add(player);
		}
	}
}